﻿Imports Crystal.CLL
Imports Infragistics.Win.UltraWinGrid
Imports Infragistics.Win
Imports CrystalDataHub.BLL
Imports Crystal.CustomControls

Public Class MergersAndAcquisitions

#Region "Declare"
    Public mintProjectID As Integer
    Private mstrScreenName As String = Me.Name
#End Region

#Region "Form Load"
    Private Sub MergersAndAcquisitions_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Dispose()
    End Sub

    ''' <summary>
    ''' Set the Fetaures on load of form.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MergersAndAcquisitions_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        FormLoadFunctionality()
    End Sub
#End Region

#Region "Events"

    ''' <summary>
    ''' To close the form.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    ''' <summary>
    ''' Message is set on text box to 'Press enter to start search'.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtSearchGrid_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearchGrid.Enter
        If txtSearchGrid.Text.Trim = MessageManager.GetMessage("DEFAULTSEARCHTEXT").Trim Then
            txtSearchGrid.Text = String.Empty
        End If
    End Sub

    ''' <summary>
    ''' Start the search on press of enter.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtSearchGrid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSearchGrid.KeyDown
        If e.KeyCode = Keys.Enter Then
            LoadMAInvestors()
        End If
    End Sub

    ''' <summary>
    ''' To expand the collapsed rows.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdMA_InitializeGroupByRow(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeGroupByRowEventArgs) Handles grdMA.InitializeGroupByRow
        e.Row.ExpansionIndicator = Infragistics.Win.UltraWinGrid.ShowExpansionIndicator.Never
        e.Row.Description = e.Row.Value
        e.Row.Expanded = True
        e.Row.PerformAutoSize()
    End Sub

    ''' <summary>
    ''' Set the M and A header Fixed and Apply Group By using Search Code. 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdMA_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles grdMA.InitializeLayout
        e.Layout.UseFixedHeaders = True
        e.Layout.Override.FixedHeaderIndicator = FixedHeaderIndicator.None
        e.Layout.Bands(0).Columns("M&A").Header.Fixed = True
        'e.Layout.Bands(0).Columns("MA").Header.Fixed = True
        e.Layout.Bands(0).Columns("Origin").CellActivation = Activation.NoEdit
        grdMA.DisplayLayout.GroupByBox.Hidden = True
        grdMA.DisplayLayout.ViewStyleBand = Infragistics.Win.UltraWinGrid.ViewStyleBand.OutlookGroupBy
        grdMA.DisplayLayout.Bands(0).SortedColumns.Add("Search Code", False, True)
        grdMA.DisplayLayout.Bands(0).Groups.Band.HeaderVisible = False
        grdMA.DisplayLayout.Override.HeaderPlacement = Infragistics.Win.UltraWinGrid.HeaderPlacement.FixedOnTop

        If Not grdMA.DisplayLayout.Appearances.Exists("ToolTipInfo") Then
            Dim lToolTipInfoAppereance As Infragistics.Win.Appearance = e.Layout.Appearances.Add("ToolTipInfo")
            lToolTipInfoAppereance.ForeColor = Color.FromArgb(246, 214, 115)
        End If

    End Sub

    ''' <summary>
    ''' Set the tool tip. Applicable for Expandable rows only.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdMA_MouseEnterElement(ByVal sender As Object, ByVal e As Infragistics.Win.UIElementEventArgs) Handles grdMA.MouseEnterElement
        Dim lugrow As UltraGridRow
        Dim tipInfo As UltraWinToolTip.UltraToolTipInfo
        Try
            lugrow = e.Element.GetContext(GetType(Infragistics.Win.UltraWinGrid.UltraGridRow))
            If lugrow Is Nothing Then Exit Sub
            If Not lugrow.IsExpandable Then
                UltraToolTipManager1.Enabled = False
                Exit Sub
            Else
                tipInfo = New UltraWinToolTip.UltraToolTipInfo(MessageManager.GetMessage(DBResult.GRID_RIGHTCLICK_COPYOPTION.ToString), ToolTipImage.Info, "Copy search code", DefaultableBoolean.True)
                tipInfo.Appearance.BackColor = grdMA.DisplayLayout.Appearances("ToolTipInfo").BackColor ' Color.FromArgb(246, 214, 115)
                UltraToolTipManager1.Enabled = True
                UltraToolTipManager1.SetUltraToolTip(sender, tipInfo)
                UltraToolTipManager1.ShowToolTip(sender)
                Exit Sub
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        Finally
            lugrow = Nothing
            tipInfo = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' Disable the tool tip on leave of expandable rows.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdMA_MouseLeaveElement(ByVal sender As Object, ByVal e As Infragistics.Win.UIElementEventArgs) Handles grdMA.MouseLeaveElement
        UltraToolTipManager1.Enabled = False
    End Sub

    ''' <summary>
    ''' Show the context menu to copy the source code. applicable if the expandable row is selected. And also user press the right click.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdMA_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grdMA.MouseUp
        Dim mousePoint As Point = Nothing
        Try
            If e.Button = Windows.Forms.MouseButtons.Right Then
                mousePoint = New Point(e.X, e.Y)
                ShowContextMenu(mousePoint)
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, mintProjectID)
        Finally
            mousePoint = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' If text box is empty fill the text Press enter to search.s
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtSearchGrid_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearchGrid.LostFocus
        If txtSearchGrid.Text.Trim = String.Empty Then
            txtSearchGrid.Text = MessageManager.GetMessage("DEFAULTSEARCHTEXT").Trim
            pnlRButtons.Focus()
        End If
    End Sub

    ''' <summary>
    ''' Laod the Investors when user click on radio button.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub rbAcquisitions_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbAcquisitions.Click
        LoadMAInvestors()
    End Sub

    ''' <summary>
    ''' Laod the Investors when user click on radio button.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub rbMergers_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbMergers.Click
        LoadMAInvestors()
    End Sub
#End Region

#Region "Methods"
    ''' <summary>
    ''' Load the data on load of form.
    ''' </summary>
    ''' <remarks></remarks>
    ''' 
    Private Sub FormLoadFunctionality()
        Me.WindowState = FormWindowState.Maximized
        Me.Text = TitleManager.GetTitle("MergerAndAcquisitions")
        txtSearchGrid.Text = MessageManager.GetMessage("DEFAULTSEARCHTEXT")
        cmbSearchField.DataSource = FillSearchCombo()
        cmbSearchField.Text = "Investor Name"
        rbMergers.Checked = True
        LoadMAInvestors()
        grdMA.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect
    End Sub

    ''' <summary>
    ''' Fill the combo box use to search.
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function FillSearchCombo() As List(Of String)
        Dim listSearchFields As List(Of String)
        Dim marrComboSearchFields() As String = {"M&A", "Origin", "Status", "Investor Name", "Principal Investor", _
                                                "Type", "Style", "Turnover", "City", "Country"}
        Try
            listSearchFields = New List(Of String)
            For i = 0 To marrComboSearchFields.Length - 1
                listSearchFields.Add(marrComboSearchFields(i))
            Next
            Return listSearchFields
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, mintProjectID)
            Return Nothing
        Finally
            listSearchFields = Nothing
        End Try
    End Function

    ''' <summary>
    ''' Load the M and A Investors
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadMAInvestors()
        Dim lintSearchin As Integer
        Dim lshortLookFor As Short
        Dim ldtMandA As DataTable
        Dim lclsMAManager As MandAManager
        Dim lstrSearchField As String = String.Empty
        Try
            lstrSearchField = txtSearchGrid.Text
            If lstrSearchField = MessageManager.GetMessage("DEFAULTSEARCHTEXT").Trim Then
                lstrSearchField = String.Empty
            End If
            Me.Cursor = Cursors.WaitCursor
            Application.DoEvents()
            '*************************************************************
            Select Case cmbSearchField.Value.ToLower
                Case "m&a"
                    lintSearchin = MergeAcquire.MA
                Case "origin"
                    lintSearchin = MergeAcquire.Origin
                Case "status"
                    lintSearchin = MergeAcquire.Status
                Case "investor name"
                    lintSearchin = MergeAcquire.InvestroName
                Case "principal investor"
                    lintSearchin = MergeAcquire.PrincipalInvestor
                Case "type"
                    lintSearchin = MergeAcquire.Type
                Case "style"
                    lintSearchin = MergeAcquire.Style
                Case "turnover"
                    lintSearchin = MergeAcquire.Turnover
                Case "city"
                    lintSearchin = MergeAcquire.City
                Case "country"
                    lintSearchin = MergeAcquire.Country
            End Select

            If rbAcquisitions.Checked Then
                lshortLookFor = MergeOrAcquire.Acquire
            Else
                lshortLookFor = MergeOrAcquire.Merger
            End If
            ldtMandA = New DataTable
            lclsMAManager = New CrystalDataHub.BLL.MandAManager
            ldtMandA = lclsMAManager.GetMandASearch(lintSearchin, lstrSearchField, lshortLookFor, mintProjectID)
            If CommonCLL.CheckEmptyDataTable(ldtMandA) Then
                ShowContextMenu()
                MessageBox.Show(MessageManager.GetMessage("NORECORD"), BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            Else
                grdMA.DataSource = ldtMandA
                SetWidthOfColumns()
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, mintProjectID)
        Finally
            Me.Cursor = Cursors.Default
            ldtMandA = Nothing
            lclsMAManager = Nothing
            lintSearchin = Nothing
            lshortLookFor = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' Set the width of Investor NAme and Principal Investor
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetWidthOfColumns()
        If grdMA.DataSource Is Nothing Then Exit Sub
        If grdMA.Rows.Count = 0 Then Exit Sub
        grdMA.DisplayLayout.Rows.Band.Columns("Investor Name").PerformAutoResize()
        grdMA.DisplayLayout.Rows.Band.Columns("Principal Investor").PerformAutoResize()
    End Sub

    ''' <summary>
    ''' Context menu is visible if the row is expanable.
    ''' </summary>
    ''' <param name="mousePoint"></param>
    ''' <remarks></remarks>
    Private Sub ShowContextMenu(ByVal mousePoint As Point)
        If grdMA.DataSource Is Nothing Then Exit Sub
        If grdMA.ActiveRow Is Nothing Then Exit Sub
        If grdMA.ActiveRow.IsExpandable Then
            cCopyRowMenu.Show(grdMA, mousePoint)
        End If
    End Sub

    ''' <summary>
    ''' If user click the copy of context menu. Source code is set on clipboard to paste the source code.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub CopyToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        If grdMA.Selected.Rows.Count = 0 Then Exit Sub
        If grdMA.Selected.Rows.Count > 1 Then Exit Sub
        Clipboard.SetText(DirectCast(grdMA.Selected.Rows.All(0), UltraGridGroupByRow).Value.ToString)
    End Sub

    ''' <summary>
    ''' Show context menu for Grid.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ShowContextMenu()
        Dim ldtGridColumns As DataTable = Nothing
        Dim marrComboSearchFields() As String = {"M&A", "Search Code", "Origin", "Status", "Investor Name", "Principal Investor", _
                                               "Type", "Style", "Turnover", "City", "Country"}
        Try
            ldtGridColumns = New DataTable
            For i = 0 To marrComboSearchFields.Length - 1
                ldtGridColumns.Columns.Add(marrComboSearchFields(i))
            Next
            grdMA.DataSource = ldtGridColumns
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, mintProjectID)
        Finally
            ldtGridColumns = Nothing
        End Try
    End Sub

#End Region
End Class